package ru.arriah.servicedesk.web.client.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.IntWrapper;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestStatusBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class SearchRequestAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;

   public SearchRequestAction() throws NamingException {
      super();

      requestManagerHome =
         Utils.getRequestManagerHomeInterface();
   }   

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {
        int page = idVal(request.getParameter("page"));        
        int itemsPerPage = Integer.parseInt(config.getProperty("ITEMS_PER_PAGE"));
        IntWrapper records = new IntWrapper(0);
        
        int clientId = Utils.getUserId(request);        
        
        RequestBean requestTemplate = new RequestBean();
        RequestStatusBean requestStatusBean = new RequestStatusBean();
        requestStatusBean.setId(idVal(request.getParameter("status")));
        
        requestTemplate.setRequestStatus(requestStatusBean);
        requestTemplate.setDispatcherText(request.getParameter("text"));
        requestTemplate.setClientText(request.getParameter("text"));
        requestTemplate.setId(idVal(request.getParameter("id")));
        requestTemplate.setRegNumber(request.getParameter("regnumber"));
        requestTemplate.setClientId(clientId);        
        
        String orderBy = request.getParameter("orderBy");
        String order = request.getParameter("order");
        order = (order == null) ? "" : order;
        orderBy = (orderBy == null) ? "" : orderBy;
        
        RequestManagerLocalObject requestManager = 
          requestManagerHome.create();
                        
        Collection<RequestBean> requestsList = 
          requestManager.searchRequestsByClient(
            requestTemplate,             
            orderBy, 
            order,
            page,
            itemsPerPage,
            records,
            clientId
          );
        
        this.pageNavigationToRequest(
          page,          
          records.getValue(), 
          "clientui?_action=search&text="+requestTemplate.getClientText()+
          "&status="+requestTemplate.getRequestStatus().getId()+
          "&id="+(requestTemplate.getId() == 0 ? "" : requestTemplate.getId())+
          "&regnumber="+requestTemplate.getRegNumber()+
          "&orderBy="+orderBy+"&order="+order+"&page=",
          request
        );

        Collection<RequestStatusBean> statusList = requestManager.selectRequestStatuses();
        
        requestManager.remove();
                
        request.setAttribute("statusList", statusList);
        request.setAttribute(
           "searchAttributes", 
           "&text="+requestTemplate.getClientText()+
           "&status="+requestTemplate.getRequestStatus().getId()+
           "&id="+(requestTemplate.getId() == 0 ? "" : requestTemplate.getId())+
           "&regnumber="+requestTemplate.getRegNumber()
        );

        request.setAttribute("action", new String("search"));
        request.setAttribute("requestsList", requestsList);
        request.setAttribute("order", (order.equals("ASC")) ? "DESC" : "ASC");
                
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}